﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Xml;
using System.IO;

/// <summary>
/// Summary description for utility
/// </summary>
public class utility2
{
	public utility2()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static RE_User PharseXML(string XMLUsuario) { 
        RE_User result=new RE_User();
        try {
            XmlDocument Xdoc = new XmlDocument();
            
            Xdoc.Load(new StringReader(XMLUsuario));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name){
                    case "usuarioid": {
                        result.userID = campo.InnerText;
                        break;
                    }
                    case "contrasena": {
                        result.pss = campo.InnerText;
                        break;
                    }
                    case "aplicacionid": {
                        result.appl = campo.InnerText;
                        break;
                    }
                    case "perfilid": {
                        result.perfil = campo.InnerText;
                        break;
                    }
                }
            }
        } catch (Exception e){
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(DateTime.Now.ToString()+   e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }

    public static RE_User PharseXML_Registro(string XMLUsuario)
    {
        RE_User result = new RE_User();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLUsuario));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "user":
                        {
                            result.userID = campo.InnerText;
                            break;
                        }
                    case "pass":
                        {
                            result.pss = campo.InnerText;
                            break;
                        }
                    case "name":
                        {
                            result.nombre = campo.InnerText;
                            break;
                        }
                    case "lastname":
                        {
                            result.apellido = campo.InnerText;
                            break;
                        }
                    case "comp-name":
                        {
                            result.compania = campo.InnerText;
                            break;
                        }
                    case "alias":
                        {
                            result.alias = campo.InnerText;
                            break;
                        }
                    case "email":
                        {
                            result.email = campo.InnerText;
                            break;
                        }
                    case "title":
                        {
                            result.title = campo.InnerText;
                            break;
                        }
                    case "estado":
                        {
                            result.estado = int.Parse(campo.InnerText);
                            break;
                        }
                    case "aplicacionid":
                        {
                            result.appl = campo.InnerText;
                            break;
                        }
                    case "perfilid":
                        {
                            result.perfil = campo.InnerText;
                            break;
                        }
                    case "group":
                        {
                            result.group = campo.InnerText;
                            break;
                        }
                    case "existe":
                        {
                            result.existe = 1;
                            break;
                        }
                    case "logo":
                        {
                            result.logo = campo.InnerText;
                            break;
                        }
                }
                if (campo.Name.Equals("direcciones"))
                {
                    XmlNodeList direcciones = campo.ChildNodes;
                    foreach (XmlElement direccion in direcciones)
                    {
                        RE_Direccion dir = new RE_Direccion();
                        foreach (XmlElement elemento in direccion)
                        {
                            if (!elemento.InnerText.Equals(""))
                            {
                                switch (elemento.Name)
                                {
                                    case "uidID":
                                        {
                                            dir.uinID = int.Parse(elemento.InnerText);
                                            break;
                                        }
                                    case "add1":
                                        {
                                            dir.stnumber = elemento.InnerText;
                                            break;
                                        }
                                    case "add2":
                                        {
                                            dir.stadd = elemento.InnerText;
                                            break;
                                        }
                                    case "city":
                                        {
                                            dir.city = elemento.InnerText;
                                            break;
                                        }
                                    case "state":
                                        {
                                            dir.state = elemento.InnerText;
                                            break;
                                        }
                                    case "zip":
                                        {
                                            dir.zip = elemento.InnerText;
                                            break;
                                        }
                                    case "office-phone":
                                        {
                                            dir.telof = elemento.InnerText;
                                            break;
                                        }
                                    case "cell-phone":
                                        {
                                            dir.telcel = elemento.InnerText;
                                            break;
                                        }
                                    case "fax":
                                        {
                                            dir.fax = elemento.InnerText;
                                            break;
                                        }
                                    case "tipo":
                                        {
                                            dir.tipodir = int.Parse(elemento.InnerText);
                                            break;
                                        }
                                }
                            }
                            if (elemento.Name.Equals("credit_card"))
                            {
                                XmlNodeList tarjetas = elemento.ChildNodes;
                                RE_CreditCard cc = new RE_CreditCard();
                                foreach (XmlElement tarjeta in tarjetas)
                                {
                                    
                                    if (!tarjeta.InnerText.Equals(""))
                                    {
                                        switch (tarjeta.Name)
                                        {
                                            case "tacID":
                                                {
                                                    cc.tacID = int.Parse(tarjeta.InnerText);
                                                    break;
                                                }
                                            case "namecreditcard":
                                                {
                                                    cc.name = tarjeta.InnerText;
                                                    break;
                                                }
                                            case "creditcard":
                                                {
                                                    cc.number = tarjeta.InnerText;
                                                    break;
                                                }
                                            case "expdate":
                                                {
                                                    cc.expdate = tarjeta.InnerText;
                                                    break;
                                                }
                                            case "ccv":
                                                {
                                                    cc.ccv = int.Parse(tarjeta.InnerText);
                                                    break;
                                                }
                                        }
                                    }
                                }
                                if (dir.tarjetas == null) dir.tarjetas = new ArrayList();
                                dir.tarjetas.Add(cc);
                            }
                        }
                        if (result.direcciones == null) result.direcciones = new ArrayList();
                        result.direcciones.Add(dir);
                    }
                }
            }
        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }



    internal static RE_GenericBean2 PharseXML_Company(string XMLCompany)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLCompany));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "id":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }
                    case "estado":
                        {
                            result.intC2 = int.Parse(campo.InnerText);
                            break;
                        }
                    case "url":
                        {
                            result.strC2 = campo.InnerText;
                            break;
                        }
                    case "contacts":
                        {
                            result.strC3 = campo.InnerText;
                            break;
                        }
                    case "phone":
                        {
                            result.strC4 = campo.InnerText;
                            break;
                        }
                   
                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }
    internal static RE_GenericBean2 PharseXML_Account(string XMLAccount)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLAccount));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }
                    case "initial":
                        {
                            result.strC2 = campo.InnerText;
                            break;
                        }

                    case "estado":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }

                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }
    internal static RE_GenericBean2 PharseXML_TEmployee(string XMLTEmployee)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLTEmployee));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }
                   
                    case "catalogo":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }

                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }

    internal static int PharseXML_UserData(string XMLDATA)
    {
        RE_GenericBean2 result;
        int resp = 0;
        int group=0;
        int[] matperfil= new int[50];
        int[] matcompany= new int[50];
        ArrayList perPaginas;
        int[] matstate = new int[50];
        int[] matcounty = new int[50];
        string[] matviewdef = new string[50];
        string [][] matvprices= new string[50][];
        string [][] matcprices= new string[50][];
        string usuarioid="";
        int posperfil = 0;
        int poscompany=0;
        int posoption = 0;
        int poscounty = 0;
        int posviewdef = 0;
        int posvprices=0;
        int poscprices=0;
        int posstate = 0;
        try
        {
            XmlDocument Xdoc = new XmlDocument();
            result= new RE_GenericBean2();
            Xdoc.Load(new StringReader(XMLDATA));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "group":
                        {
                            group = int.Parse(campo.InnerText);
                            break;
                         }
                    case "userid":
                        {
                            usuarioid=(campo.InnerText);
                            break;
                         }
                }
                if (campo.Name.Equals("perfiles"))
                {
                    
                    XmlNodeList perfiles = campo.ChildNodes;
                    posperfil = 0;
                    foreach (XmlElement perfil in perfiles)
                    {
                       
                                switch (perfil.Name)
                                {
                                    case "perfil":
                                        {
                                            matperfil[posperfil] = int.Parse(perfil.InnerText);
                                            posperfil++;
                                            break;
                                        }
                                }
                            }
                    if (group == 3) { DB2.delete("tbl_usuario_perfil", "upe_usu_id", usuarioid,0,1); DB2.setPerfilUsuario(usuarioid, matperfil,posperfil,1); }
                    else if (group== 1) {matperfil[0]=1; posperfil=1; DB2.delete("tbl_usuario_perfil", "upe_usu_id", usuarioid,0,1); DB2.setPerfilUsuario(usuarioid, matperfil,posperfil,1); }
                        }
                
                
                
                if (campo.Name.Equals("ClientCompanies"))
                {
                    poscompany=0;
                    XmlNodeList companies = campo.ChildNodes;
                    foreach (XmlElement company in companies)
                    {

                        switch (company.Name)
                        {
                            case "company":
                                {
                                    matcompany[poscompany]= int.Parse(company.InnerText);
                                    poscompany++;
                                    break;
                                }
                        }
                    }
                     DB2.delete("tbl_usuario_company", "uco_usu_id", usuarioid,0,1);
                     DB2.setCompanyporUsuario(usuarioid, matcompany,poscompany);
                }

                if (campo.Name.Equals("OptionsAccess"))
                {
                    perPaginas = new ArrayList();
                    RE_GenericBean2 pPage = null;
                    XmlNodeList options = campo.ChildNodes;
                    foreach (XmlElement option in options)
                    {
                         
                        switch (option.Name)
                        {
                            case "pageID":
                                {
                                    pPage = new RE_GenericBean2();
                                    pPage.intC1 = int.Parse(option.InnerText);
                                    break;
                                }
                            case "permit":
                                {
                                    pPage.int64C1 = long.Parse(option.InnerText);
                                    perPaginas.Add(pPage);
                                    break;
                                }
                        }
                    }

                    foreach (RE_GenericBean2 pPage2 in perPaginas)
                    {
                        int resul = DB2.updateDataUsuPermiso("permiso", usuarioid, pPage2);
                    }
                }

                if (campo.Name.Equals("ViewDefault"))
                {
                    XmlNodeList viewdefaul = campo.ChildNodes;
                    string where = "and (";
                    int p = 0;
                    foreach (XmlElement viewde in viewdefaul)
                    {

                        switch (viewde.Name)
                        {
                            case "view":
                                {
                                    
                                    if (p == 1) { where += " or "; }else{ p = 1; }
                                    where += " client_id= '" + viewde.InnerText.Trim()+"' ";
                                    break;
                                }
                            case "viewno":
                                {

                                   // if (p == 1) { where += " and "; } else { p = 1; }
                                   // where += " client_id != '" + viewde.InnerText.Trim() + "' ";
                                    break;
                                }
                        }
                    }
                    where+= " )";
                    if (where.Equals("and ( )")) where = "";
                    DB2.updatedata("tbl_usuario", "usu_where", "usu_id", usuarioid, 0, where, 0, 1);
                }

                if (campo.Name.Equals("RestrictedAccess"))
                {
                    perPaginas = new ArrayList();
                    RE_GenericBean2 pPage = null;
                    XmlNodeList restrictedacc = campo.ChildNodes;
                    foreach (XmlElement res in restrictedacc)
                    {

                        switch (res.Name)
                        {
                            case "pageID":
                                {
                                    pPage = new RE_GenericBean2();
                                    pPage.intC1 = int.Parse(res.InnerText);
                                    break;
                                }
                            case "permit":
                                {
                                    pPage.int64C1 = long.Parse(res.InnerText);
                                    perPaginas.Add(pPage);
                                    break;
                                }
                        }
                    }

                    foreach (RE_GenericBean2 pPage2 in perPaginas)
                    {
                        int resul = DB2.updateDataUsuPermiso("restricted", usuarioid, pPage2);
                    }
                }

                if (campo.Name.Equals("FilterView"))
                {
                    string where = "and ("; int p = 0;
                    XmlNodeList filterview = campo.ChildNodes;
                    foreach (XmlElement fil in filterview)
                    {

                        switch (fil.Name)
                        {
                            case "product":
                                {
                                   // result.intC1 = int.Parse(fil.InnerText);
                                   // if (p == 1) { where += " or "; } else { p = 1; }
                                   // where += " orden.product_id = " + fil.InnerText.Trim();
                                    break;
                                }
                            case "productno":
                                {
                                    result.intC1 = int.Parse(fil.InnerText);
                                    if (p == 1) { where += " and "; } else { p = 1; }
                                    where += " [ORDER].product_id != " + fil.InnerText.Trim();
                                    break;
                                }
                        }
                    }
                    where += " )";
                    if (where.Equals("and ( )")) where = "";
                    DB2.updatedata("tbl_usuario", "usu_where3", "usu_id", usuarioid, 0, where,0, 1);
                }

                if (campo.Name.Equals("CompanyAccess"))
                {
                    string where = "and ("; int p = 0;
                    XmlNodeList clientcompanies = campo.ChildNodes;
                    foreach (XmlElement company in clientcompanies)
                    {

                        switch (company.Name)
                        {
                            case "company":
                                {
                                   // if (p == 1) { where += " or "; } else { p = 1; }
                                    //where += " orden.company = " + company.InnerText.Trim();
                                    break;
                                }
                            case "companyno":
                                {
                                    if (p == 1) { where += " or "; } else { p = 1; }
                                    where += " company != " + company.InnerText.Trim();
                                    break;
                                }
                        }
                    }
                    where += " )";
                    if (where.Equals("and ( )")) where = "";
                    DB2.updatedata("tbl_usuario", "usu_where2", "usu_id", usuarioid, 0, where, 0, 1);
                }

                if (campo.Name.Equals("ClientAccounts"))
                {
                    poscompany = 0;
                    string where = "and ("; int p = 0;
                    XmlNodeList clientacc = campo.ChildNodes;
                    foreach (XmlElement acc in clientacc)
                    {

                        switch (acc.Name)
                        {
                            case "account":
                                {
                                    matcompany[poscompany] = int.Parse(acc.InnerText);
                                    poscompany++;
                                    if (p == 1) { where += " or "; } else { p = 1; }
                                    where += " account = " + acc.InnerText;
                                    break;
                                }
                        }
                    }
                    DB2.delete("tbl_usuario_account", "uacc_usu_id", usuarioid, 0, 1);
                    DB2.setAccountporUsuario(usuarioid, matcompany, poscompany);
                    
                }



                if (campo.Name.Equals("VendorStates"))
                {
                    posstate = 0;
                    XmlNodeList states = campo.ChildNodes;
                    foreach (XmlElement state in states)
                    {

                        switch (state.Name)
                        {
                            case "state":
                                {
                                    
                                    matstate[posstate]= int.Parse(state.InnerText);
                                    posstate++;
                                    break;
                                }
                        }
                    }
                    DB2.delete("tbl_usuario_state", "ust_usu_id", usuarioid, 0, 1);
                    DB2.setStateporUsuario(usuarioid, matstate, posstate);
                }




                if (campo.Name.Equals("VendorCounties"))
                {
                    poscounty = 0;
                    XmlNodeList counties = campo.ChildNodes;
                    foreach (XmlElement county in counties)
                    {

                        switch (county.Name)
                        {
                            case "county":
                                {
                                   
                                    matcounty[poscounty] = int.Parse(county.InnerText);
                                    poscounty++;
                                    break;
                                }
                        }
                    }

                    DB2.delete("tbl_usuario_county", "ucou_usu_id", usuarioid, 0, 1);
                    DB2.setCountyporUsuario(usuarioid, matcounty, poscounty);
                }

                if (campo.Name.Equals("ClientPrices"))
                {
                    posvprices = 0; matvprices[posvprices] = new string[2];
                    XmlNodeList clientprices = campo.ChildNodes;
                    foreach (XmlElement price in clientprices)
                    {
                        
                        switch (price.Name)
                        {
                            case "product":
                                {
                                    matvprices[posvprices][0] = price.InnerText;
                                    break;
                                }
                            case "price":
                                {
                                    matvprices[posvprices][1] = price.InnerText;
                                    posvprices++;
                                    matvprices[posvprices] = new string[2];
                                    break;
                                }
                                
                        }
                    }
                    if (posvprices > 0)
                    {
                        DB2.delete("tbl_usuario_precio", "upr_usu_id", usuarioid, 0, 1);
                        DB2.setPrecioporUsuario(usuarioid, matvprices, posvprices);
                    }
                }

            
                    if (campo.Name.Equals("VendorPrices"))
                {
                    posvprices=0;
                    matvprices[posvprices] = new string[2];
                    XmlNodeList vendorprices = campo.ChildNodes;
                    foreach (XmlElement price in vendorprices)
                    {
                       
                        switch (price.Name)
                        {
                            case "product":
                                {
                                    
                                    matvprices[posvprices][0] = price.InnerText;
                                    break;
                                }
                            case "price":
                                {
                                    matvprices[posvprices][1] = price.InnerText;
                                    posvprices++;
                                    matvprices[posvprices] = new string[2];
                                    break;
                                }
                        }
                    }
                    if (posvprices > 0)
                    {
                        DB2.delete("tbl_usuario_precio", "upr_usu_id", usuarioid, 0, 1);
                        DB2.setPrecioporUsuario(usuarioid, matvprices, posvprices);
                    }
                   }
            }

            resp = 1;
        }
            

        
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            resp = 0;
      
        }
        return resp;
    }

    internal static int PharseXML_PerfilData(string XMLDATA)
    {
        int  result;
        ArrayList array = new ArrayList();
       
        long[,] matperfilyes = new long[2,200];
        long[,] matperfilno = new long[2,200];
        long[,] matresyes = new long[2,200];
        long[,] matresno = new long[2,200];
        int posresyes = 0; int posresno = 0;
        string perfilname = ""; string pageid = "";
        int pospermisoyes = 0; int pospermisono = 0;
        try
        {
            XmlDocument Xdoc = new XmlDocument();
            result = 0;
            Xdoc.Load(new StringReader(XMLDATA));
            int perfilid = 0; int restrictedprop = 0;
            int app = 0; long restricted = 0; long permiso = 0;
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                if (campo.Name.Equals("app"))
                {
                    app = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("perfilid"))
                {
                    perfilid = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("perfilname"))
                {
                    perfilname = campo.InnerText;
                }
                if (campo.Name.Equals("page"))
                {
                   

                    XmlNodeList page = campo.ChildNodes;
                    foreach (XmlElement pa in page)
                    {

                        if (pa.Name.Equals("pageid"))
                        {
                            pageid = pa.InnerText;
                        }
                    

                    if (pa.Name.Equals("OptionsAccess"))
                    {
                        permiso = 0;

                        XmlNodeList restrictedacc = pa.ChildNodes;
                        foreach (XmlElement res in restrictedacc)
                        {

                            switch (res.Name)
                            {
                                case "optionyes":
                                    {

                                        permiso += long.Parse(res.InnerText);
                                       
                                        
                                        break;

                                    }
                                case "pageyes":
                                    {

                                        permiso += long.Parse(res.InnerText);
                                       
                                        break;

                                    }
                                case "optionno":
                                    {
                                        matperfilyes[0, pospermisoyes] = long.Parse(pageid);
                                        matperfilno[1,pospermisono] = long.Parse(res.InnerText);
                                        pospermisono++;
                                        break;

                                    }
                            }

                        }


                    }

                    if (pa.Name.Equals("RestrictedAccess"))
                    {
                        restricted = 0;

                        XmlNodeList restrictedacc = pa.ChildNodes;
                        foreach (XmlElement res in restrictedacc)
                        {

                            switch (res.Name)
                            {
                                case "optionyes":
                                    {
                                        long item = long.Parse(res.InnerText);
                                        restricted += long.Parse(res.InnerText);
                                       
                                       
                                        break;
                                    }
                                case "pageyes":
                                    {
                                        long item = long.Parse(res.InnerText);
                                        restricted += long.Parse(res.InnerText);

                                       
                                        break;
                                    }
                                case "optionno":
                                    {
                                      
                                        posresno++;
                                        break;
                                    }
                            }

                        }


                    }
                    
                    }// si es campo page
                    matperfilyes[0, pospermisoyes] = long.Parse(pageid);
                    matperfilyes[1, pospermisoyes] = permiso;
                    matresyes[0, posresyes] = long.Parse(pageid);
                    matresyes[1, posresyes] = restricted;
                    pospermisoyes++; posresyes++;
                   
                }
               
            }
            array.Add(matperfilyes);
            array.Add(matresyes);
            //RE_GenericBean2 reold = DB2.getPerfilPerRes(perfilid.ToString(), app.ToString());
             DB2.updatePergilApp(app, perfilid, array, perfilname);
          //  DB2.updateUserPerRes(app, perfilid, matperfilyes, matperfilno, matresyes, matresno, pospermisoyes, pospermisono, posresyes, posresno, reold);
            DB2.updateUserPerRes(app, perfilid, array);
        }catch (Exception e){
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            result = 0;
        }
        return result;
    }

    internal static RE_GenericBean2 PharseXML_NewPerfil(string XMLPerfil)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLPerfil));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }
                    

                    case "permiso":
                        {
                            result.int64C1 = long.Parse(campo.InnerText);
                            break;
                        }
                    case "app":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }
                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }
    internal static RE_GenericBean2 PharseXML_NewPage(string XMLPerfil)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLPerfil));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }


                    case "permiso":
                        {
                            result.int64C1 = long.Parse(campo.InnerText);
                            break;
                        }
                    case "app":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }

                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }
    internal static int PharseXML_PageData(string XMLDATA)
    {
        int result;

        try
        {
            XmlDocument Xdoc = new XmlDocument();
            result = 0;
            Xdoc.Load(new StringReader(XMLDATA));
            int opcionid = 0; string name = "";
            int app = 0;
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                if (campo.Name.Equals("app"))
                {
                    app = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("opcionid"))
                {
                    opcionid = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("opcionname"))
                {
                    name = campo.InnerText;
                }
            }
            int p = DB2.updatePageApp(app, opcionid, name);
            result = p;
            if (p == 0) {
                string ruta = "c:\\TEMP\\archivo.txt";
                StreamWriter escritor = new StreamWriter(ruta, true);
                escritor.WriteLine("No modifico PageeAPP ");
                escritor.Close();
                result = 0;
            }

            
        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            result = 0;
        }
        return result;
    }
    internal static RE_GenericBean2 PharseXML_NewOption(string XMLPerfil)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLPerfil));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }


                    case "app":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }
                    case "parent":
                        {
                            result.intC3 = int.Parse(campo.InnerText);
                            break;
                        }
                    case "section":
                        {
                            result.intC2 = int.Parse(campo.InnerText);
                            break;
                        }
                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }

    internal static int PharseXML_UpdateOpcion(string XMLDATA)
    {
        int result;

        try
        {
            XmlDocument Xdoc = new XmlDocument();
            result = 0;
            Xdoc.Load(new StringReader(XMLDATA));
            int opcionid = 0; string name = "";
            int parent = 0, section = 0;
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                if (campo.Name.Equals("parent"))
                {
                    parent = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("opcionid"))
                {
                    opcionid = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("opcionname"))
                {
                    name = campo.InnerText;
                }
                if (campo.Name.Equals("opcsection"))
                {
                    section = int.Parse(campo.InnerText);
                }
            }
            int p = DB2.updateOpcionApp(parent, opcionid, name, section);
            result = p;

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            result = 0;
        }
        return result;
    }

    internal static int PharseXML_UpdateMenu(string XMLDATA)
    {
        int result;

        try
        {
            XmlDocument Xdoc = new XmlDocument();
            result = 0;
            Xdoc.Load(new StringReader(XMLDATA));
            string name = "", url = "";
            int id = 0, table = 0;
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                if (campo.Name.Equals("table"))
                {
                    table = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("id"))
                {
                    id = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("name"))
                {
                    name = campo.InnerText;
                }
                if (campo.Name.Equals("url"))
                {
                    url = campo.InnerText;
                }
            }
            int p = DB2.updateOppMenu(table, id, name, url);
            result = p;

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            result = 0;
        }
        return result;
    }

    internal static int PharseXML_DeleteMenu(string XMLDATA)
    {
        int result;

        try
        {
            XmlDocument Xdoc = new XmlDocument();
            result = 0;
            Xdoc.Load(new StringReader(XMLDATA));
            string menu = "", option = "";
            int id = 0, table = 0;
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                if (campo.Name.Equals("table"))
                {
                    table = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("id"))
                {
                    id = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("menu"))
                {
                    menu = campo.InnerText;
                }
                if (campo.Name.Equals("option"))
                {
                    option = campo.InnerText;
                }
            }
            int p = DB2.deleteOppMenu(table, id, menu, option);
            result = p;

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            result = 0;
        }
        return result;
    }

    internal static int PharseXML_AddMenu(string XMLDATA)
    {
        int result;

        try
        {
            XmlDocument Xdoc = new XmlDocument();
            result = 0;
            Xdoc.Load(new StringReader(XMLDATA));
            string menu = "", option = "", name = "", url = "";
            int id = 0, table = 0, idoption = 0;
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                if (campo.Name.Equals("table"))
                {
                    table = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("id"))
                {
                    id = int.Parse(campo.InnerText);
                }
                if (campo.Name.Equals("menu"))
                {
                    menu = campo.InnerText;
                }
                if (campo.Name.Equals("option"))
                {
                    option = campo.InnerText;
                }
                if (campo.Name.Equals("name"))
                {
                    name = campo.InnerText;
                }
                if (campo.Name.Equals("url"))
                {
                    url = campo.InnerText;
                }
                if (campo.Name.Equals("idoption"))
                {
                    idoption = int.Parse(campo.InnerText);
                }
            }
            int p = DB2.addOppMenu(table, id, menu, option, name, url, idoption);
            result = p;

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            result = 0;
        }
        return result;
    }

    public static string toCapital(string strin)
    {
        int cont = 0;
        string result = "";
        bool encontrado = false;
        strin = strin.TrimStart();
        strin = strin.TrimEnd();
        string[] cad = strin.Split(' ');

        for (int aux = 0; aux < strin.Length; aux++)
        {
            if (strin[aux] == ' ')
            {
                encontrado = true;
            }
        }

        if (strin != null)
        {
            if (encontrado == true)
            {
                while (cont < cad.Length)
                {
                    if (cad[cont] != "")
                        result = result + " " + cad[cont].ToUpper().Substring(0, 1) + cad[cont].ToLower().Substring(1, cad[cont].ToString().Length - 1);
                    cont++;
                }
            }
            else
            {
                if (strin == "")
                    result = "";
                else
                    result = strin.ToUpper().Substring(0, 1) + strin.ToLower().Substring(1, strin.ToString().Length - 1);
            }
        }
        else
            result = "";

        return result.TrimStart();
    }

    
}
